/*
 * conver.h
 *
 *  Created on: 2011-10-12
 *      Author: shenjiong
 */

#ifndef CONVER_H_
#define CONVER_H_

#include <string>
#include <iostream>
#include <stdlib.h>

#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include "ProtoFile.h"
#include "Disktools.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include "buffclass.h"
#include "bt_mutex.h"
#include "bt_thread.h"
#include "saft_deque.h"
#include "sndaconfig.h"
#include "conver.h"

template<class T>
void converfile(std::string srcfile, std::string destfile)
{
    FILE * fp = fopen(srcfile.c_str(), "rb");
    WriteProtoFile b(destfile);

    bool finished;
    std::cout << "begin read " << std::endl;

    BuffClass Buffer;
    bool fileend = false;
    while(1)
    {
        long long canreadlen = Buffer.CanAddBufflen();

        long long readedlen = fread(Buffer.GetBuff(), 1, canreadlen, fp);

        if(readedlen == 0)
        {
            fileend = true;
        }
        Buffer.AddUsedData(readedlen);
        long long v = 0;

        for(;;)
        {
            char * ppoint = NULL;
            ppoint = Buffer.GetLine();

            if(ppoint)
            {
                ++v;
                T tmpt1;
                if(ReadkeybaserecordFromBuff(ppoint, tmpt1))
                {
                    b.SaveProject(tmpt1);

                }

            }
            else  // no enough data
            {
                Buffer.ClearUsedbuff();
                break;
            }
        }
        if(fileend == true)
        {
            break;
        }

    }

}


#endif /* CONVER_H_ */
